//
//  FunnyPictureViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/5/6.
//

#import "FunnyPictureViewController.h"
#import "FunPicTableViewCell.h"
#import "FunyModel.h"
#import "FunyPicDetailsViewController.h"
@interface FunnyPictureViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation FunnyPictureViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count < 20) {
        [self SQ_Header];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = SQHexColor(@"#000000", 0.4);
    self.zx_hideBaseNavBar = YES;
}

- (void)SQ_UIViewForSelfView{

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, FootHeight, 0);
    [self SQ_tableViewAddRefresh];
}
 
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:self.type forKey:@"type"];
    [HttpTool post:Url37 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [FunyModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:self.type forKey:@"type"];
    [HttpTool post:Url37 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [FunyModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}
 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FunPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FunPicTableViewCell"];
    if (!cell) {
        cell = [[FunPicTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"FunPicTableViewCell"];
    }
    FunyModel * model =  [self.jsonArray objectAtIndex:indexPath.item];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WS(weakSelf);
    [cell setIsBlackBlock:^{
        [weakSelf SQ_black:model];
    }];
  
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [FunPicTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FunyPicDetailsViewController * vc = [[FunyPicDetailsViewController alloc]init];
    vc.model = [self.jsonArray objectAtIndex:indexPath.item];
    [self SQ_pushViewControllerFull:vc];
    WS(weakSelf);
    [vc setIsRemoveBlock:^(NSString * _Nonnull userid) {
        NSMutableArray * array  = [NSMutableArray arrayWithArray:weakSelf.jsonArray];
        for (FunyModel * model in weakSelf.jsonArray) {
            if ([model.userid isEqualToString:userid]) {
                [array removeObject:model];
            }
        }
        weakSelf.jsonArray  = [NSMutableArray arrayWithArray:array];
        [weakSelf.tableView reloadData];
    }];
    [vc setIsBlackRemoveBlock:^(NSString * _Nonnull userid) {
        [weakSelf SQ_listRemoveID:userid];
    }];
}

-(void)SQ_black:(FunyModel*)model{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.uid forKey:@"pid"];
    [dic setObject:@"1" forKey:@"type"];
    [HttpTool post:Url42 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_listRemoveID:model.uid];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_listRemoveID:(NSString*)pid{
    NSMutableArray * array = [NSMutableArray new];
    for (FunyModel * model in self.jsonArray) {
        if ([model.uid isEqualToString:pid]) {
            [array addObject:model];
        }
    }
    [self.jsonArray removeObjectsInArray:array];
    [self.tableView reloadData];
}

@end
